package com.parsechina.hpush.broker.internal;

import com.parsechina.hpush.broker.spi.ServerCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 推送服务器运行器
 *
 * @author linfeng
 * @date 2019-07-24
 **/
@Component
public class PushServerRunner implements CommandLineRunner {

    private static final Logger LOG = LoggerFactory.getLogger(PushServerRunner.class);
    private final ServerCycle serverCycle;

    @Autowired
    public PushServerRunner(ServerCycle serverCycle) {
        this.serverCycle = serverCycle;
    }

    @Override
    public void run(String... args) {

        serverCycle.startup();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            LOG.info("Server stopping...");
            serverCycle.shutdown();
            LOG.info("Server stopped");
        }));

    }

}
